package leecode.栈与队列问题;

import java.util.Stack;

/**
 * @author wangxi created on 2024/8/4 21:23
 * @version v1.0
 *
 * https://leetcode.cn/problems/min-stack/
 *
 */
public class MinStack {
    private Stack<Integer> min;
    private Stack<Integer> data;

    public MinStack() {
        min = new Stack<>();
        data = new Stack<>();
    }

    public void push(int val) {
        data.push(val);
        if (min.isEmpty()) {
            min.push(val);
        } else {
            // 注意：这里可能会有相同的元素，相同的元素也需要入栈
            if (val <= min.peek()) {
                min.push(val);
            }
        }
    }

    public void pop() {
        if (data.isEmpty()) {
            return;
        }
        int val = data.peek();
        data.pop();
        if (val == min.peek()) {
            min.pop();
        }
    }

    public int top() {
        if (data.isEmpty()) {
            return -1;
        }
        return data.peek();
    }

    public int getMin() {
        if (min.isEmpty()) {
            return -1;
        }
        return min.peek();
    }
}
